package configuration;

import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.util.StringUtils;
import properties.FileProperties;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Properties;

/**
 * @author xj
 * @since 1.0
 */
@EnableConfigurationProperties(FileProperties.class)
@Slf4j
public class FileAutoConfiguration {

private final FileProperties fileProperties;

    public FileAutoConfiguration(FileProperties fileProperties) {
        this.fileProperties = fileProperties;
    }

    @PostConstruct
    public void init() {
        Properties properties = new Properties();
        if(StringUtils.isEmpty(fileProperties.getTrackerServers())){
            throw new NullPointerException("文件服务器地址不能为空");
        }else {
            properties.setProperty(ClientGlobal.PROP_KEY_TRACKER_SERVERS, fileProperties.getTrackerServers());
        }
        if(!StringUtils.isEmpty(fileProperties.getConnectTimeoutInSeconds())){
            properties.setProperty(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS,
                    fileProperties.getConnectTimeoutInSeconds());
        }
        if(!StringUtils.isEmpty(fileProperties.getNetworkTimeoutInSeconds())){
            properties.setProperty(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS,
                    fileProperties.getNetworkTimeoutInSeconds());
        }
        if(!StringUtils.isEmpty(fileProperties.getCharset())){
            properties.setProperty(ClientGlobal.PROP_KEY_CHARSET, fileProperties.getCharset());
        }
        if(!StringUtils.isEmpty(fileProperties.getHttpAntiStealToken())){
            properties.setProperty(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, fileProperties.getHttpAntiStealToken());
        }
        if(!StringUtils.isEmpty(fileProperties.getHttpSecretKey())){
            properties.setProperty(ClientGlobal.PROP_KEY_HTTP_SECRET_KEY, fileProperties.getHttpSecretKey());
        }
        if(!StringUtils.isEmpty(fileProperties.getHttpTrackerHttpPort())){
            properties.setProperty(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, fileProperties.getHttpTrackerHttpPort());
        }
        if(!StringUtils.isEmpty(fileProperties.getPoolEnabled())){
            properties.setProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_ENABLED, fileProperties.getPoolEnabled());
        }
        if(!StringUtils.isEmpty(fileProperties.getPoolMaxCountPerEntry())){
            properties.setProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_COUNT_PER_ENTRY,
                    fileProperties.getPoolMaxCountPerEntry());
        }
        if(!StringUtils.isEmpty(fileProperties.getPoolMaxIdleTime())){
            properties.setProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_IDLE_TIME,
                    fileProperties.getPoolMaxIdleTime());
        }
        if(!StringUtils.isEmpty(fileProperties.getPoolMaxWaitTimeInMS())){
            properties.setProperty(ClientGlobal.PROP_KEY_CONNECTION_POOL_MAX_WAIT_TIME_IN_MS,
                    fileProperties.getPoolMaxWaitTimeInMS());
        }

        try {
            ClientGlobal.initByProperties(properties);
        } catch (IOException | MyException e) {
            log.error("文件上传配置出错:{0}", e);
        }
    }
}
